﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class QuocGiaDAL:StandardConnectDB,IQuocGia
    {

        public List<DTO.QuocGiaDTO> GetlistQuocGia()
        {
            List<QuocGiaDTO> listQuocGia = new List<QuocGiaDTO>();
            DataTable dt = this.DTExecuteSelect("select * from QuocGia");
            foreach (DataRow dr in dt.Rows)
            {
                listQuocGia.Add(ConvertObjectDataRowToQuocGiaDTO(dr));
            }

            return listQuocGia;
            
            
        }

        public QuocGiaDTO GetQuocGiaByID(string MaQuocGia)
        {
            QuocGiaDTO quocgiaDTO = new QuocGiaDTO();
            
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaQuocGia", MaQuocGia);

            DataTable dt = this.DTExecuteSelect("SP_QuocGia_GetByID", paramlist);

            quocgiaDTO = ConvertObjectDataRowToQuocGiaDTO(dt.Rows[0]);

            return quocgiaDTO;
        }

        public bool InsertQuocGia(DTO.QuocGiaDTO quocgia)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaQuocGia", quocgia.MaQuocGia);
            paramlist.Add("@TenQuocGia", quocgia.TenQuocGia);
            paramlist.Add("@TenVietTat", quocgia.TenVietTat);
            paramlist.Add("@GhiChu", quocgia.GhiChu);
            return ExecuteUpdate("SP_QuocGia_Insert", paramlist);
            
        }

        public bool DeleteQuocGia(DTO.QuocGiaDTO quocgia)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaQuocGia", quocgia.MaQuocGia);

            return this.ExecuteUpdate("SP_QuocGia_Delete", paramlist);
            
        }

        public bool UpdateQuocGia(DTO.QuocGiaDTO quocgia)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaQuocGia", quocgia.MaQuocGia);
            paramlist.Add("@TenQuocGia", quocgia.TenQuocGia);
            paramlist.Add("@TenVietTat", quocgia.TenVietTat);
            paramlist.Add("@GhiChu", quocgia.GhiChu);
            return ExecuteUpdate("SP_QuocGia_Update", paramlist);
        }

        public QuocGiaDTO ConvertObjectDataRowToQuocGiaDTO(System.Data.DataRow dr)
        {
            QuocGiaDTO quocgia1 = new QuocGiaDTO();
            if (dr != null)
            {
                quocgia1.MaQuocGia = dr["MaQuocGia"].ToString();
                quocgia1.TenQuocGia = dr["TenQuocGia"].ToString();
                quocgia1.TenVietTat = dr["TenVietTat"].ToString();
                quocgia1.GhiChu = dr["GhiChu"].ToString();

                return quocgia1;
            }
            else
                return null;
            
        }
    }
}
